package com.bdqn.ch08.pojo;

import jakarta.annotation.Resource;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.*;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

@SpringBootTest
@RunWith(SpringRunner.class)
public class RedisTemplateTester {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private RedisTemplate redisTemplate;

    @Test
    public void testString(){
        stringRedisTemplate.opsForValue().set("name" , "czkt");
        Assert.assertEquals("czkt" , stringRedisTemplate.opsForValue().get("name"));
    }

    @Test
    public void testObj(){
        SysUser user = new SysUser("czkt" , "123456" , 1L , null);
        ValueOperations<String , SysUser>operations = redisTemplate.opsForValue();
        operations.set("com.bdqn.ch08.pojo.SysUser",user);
        SysUser u = operations.get("com.bdqn.ch08.pojo.SysUser");
        System.out.println("user.usrName:"+u.getUsrName());
    }

    @Test
    public void testExpire()throws InterruptedException{
        SysUser user = new SysUser("cskt" , "12346" , 1L , null);

        ValueOperations<String , SysUser>operations = redisTemplate.opsForValue();
        operations.set("expire" , user , 100 , TimeUnit.MILLISECONDS);
        Thread.sleep(1000);
        boolean exists = redisTemplate.hasKey("expire");
        if (exists){
            System.out.println("exists is true");
        }else {
            System.out.println("exists is false");
        }
    }

    @Test
    public void testHash(){
        HashOperations<String , Objects , Objects> hash = redisTemplate.opsForHash();
        String value = hash.get("hash" , "name").toString();
        System.out.println("hash name 'value:"+value);
    }

    @Test
    public void testList(){
        ListOperations<String , String> list = redisTemplate.opsForList();
        list.leftPop("list" , Long.parseLong("accp"));
        list.leftPop("list" , Long.parseLong("bdqn"));
        String value =  (String) list.leftPop("list");
        System.out.println("list value:"+value.toString());
    }
}
